package cn.maihe.elg.operation.web.system;

import cn.maihe.elg.operation.config.ImageUploadConfig;
import cn.maihe.elg.operation.config.TemplateUploadConfig;
import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.service.system.AttachmentService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * @author JHWei
 * 2018/7/11 下午4:55
 */
@RestController
@Slf4j
@Api(value="上传文件",tags={"上传文件"})
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    @Resource
    private ImageUploadConfig imageUploadConfig;
    @Resource
    private TemplateUploadConfig templateUploadConfig;

    @RequestMapping(value = "/api/upload/image", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上传图片,文件不能为空，大小不能超过10M")
    public String uploadImage(@RequestParam("file") MultipartFile file) {

        if (file.isEmpty()) {
            throw new SystemRuntimeException(ErrorCode.BSMAU004);
        }
        if (file.getSize() > imageUploadConfig.getMaxSize()) {
            throw new SystemRuntimeException(ErrorCode.BSMAU005);
        }

        String fileName = file.getOriginalFilename();
        String fileExt = fileName.substring(fileName.lastIndexOf(".")+1);
        fileExt = fileExt.toLowerCase();
        if (!imageUploadConfig.getType().contains(fileExt)) {
            throw  new SystemRuntimeException(ErrorCode.BSMAU006, fileExt);
        }
        // 生成一个随机的名称，避免文件名重复
        UUID uuid = UUID.randomUUID();
        // 保存文件
        File saveFile = Paths.get(imageUploadConfig.getPath() ,uuid +"."+ fileExt).toFile();
        try {
            file.transferTo(saveFile);
        } catch (IOException e) {
            log.error("上传失败",e);
            throw new SystemRuntimeException(ErrorCode.BSMAU007);
        }

        return  uuid +"."+ fileExt;
    }
    @RequestMapping(value = "/api/upload/template", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上传模板片,文件不能为空，大小不能超过10M")
    public String uploadTemplate(@RequestParam("file") MultipartFile file) {
        JSONObject result = new JSONObject();
        if (file.isEmpty()) {
            throw new SystemRuntimeException(ErrorCode.BSMAU004);
        }
        if (file.getSize() > templateUploadConfig.getMaxSize()) {
            throw new SystemRuntimeException(ErrorCode.BSMAU005);
        }

        String fileName = file.getOriginalFilename();
        result.put("name",fileName);
        String fileExt = fileName.substring(fileName.lastIndexOf(".")+1);
        fileExt = fileExt.toLowerCase();
        if (!templateUploadConfig.getType().contains(fileExt)) {
            throw  new SystemRuntimeException(ErrorCode.BSMAU006, fileExt);
        }
        // 生成一个随机的名称，避免文件名重复
        UUID uuid = UUID.randomUUID();
        // 保存文件
        File saveFile = Paths.get(templateUploadConfig.getPath(),uuid +"."+ fileExt).toFile();
        try {
            file.transferTo(saveFile);
        } catch (IOException e) {
            log.error("上传失败",e);
            throw new SystemRuntimeException(ErrorCode.BSMAU007);
        }
        result.put("url",uuid+"."+fileExt);
        return  result.toJSONString();
    }


}
